package com.yonyou.pmclouds.milestone.processor;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.milestone.entity.MilestoneVO;
import com.yonyou.pmclouds.milestone.mapper.MilestoneMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author th
 * @date 2019/11/13 10:20
 */
@Component
public class CheckCodeAndNameUpdateProcessor implements SingleProcessor<MilestoneVO> {
    @Autowired
    private MilestoneMapper mapper;

    @Override
    public void process(MilestoneVO vo, MilestoneVO orig) throws BusinessException {
        if (vo == null || orig == null)
            return;

        int count = 0;
        // 判断编码是否修改
        if (!orig.getCode().equals(vo.getCode())) {
            if (vo.getCode().length() > 20)
                throw new BusinessException("'" + vo.getCode() + "'" + "编码长度不能超过20");

            count = mapper.countCode(vo.getCode(), vo.getPkTenant());
            if (count > 0)
                throw new BusinessException("'" + vo.getCode() + "'" + "编码已被占用，请重新设置");
        }

        // 判断名称是否修改
        if (!orig.getName().equals(vo.getName())) {
            if (vo.getName().length() > 50)
                throw new BusinessException("'" + vo.getName() + "'" + "名称长度不能超过50");

            count = mapper.countName(vo.getName(), vo.getPkTenant());
            if (count > 0)
                throw new BusinessException("'" + vo.getName() + "'" + "名称已被占用，请重新设置");
        }
    }
}
